/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.post;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.fbPost;
import model.fbSession;
import model.pojo.Post;

/**
 *
 * @author Yasith
 */
public class saveExistingPost extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Post cp = null;
        try {
            cp = fbPost.getPost(Integer.parseInt(request.getParameter("postId")));
        } catch (NumberFormatException ex) {}

        fbSession fbs = (fbSession) request.getSession().getAttribute("fbSession");
        if ((fbs != null) && (cp != null) && (cp.getUserID().equals(fbs.getUser()))) {

            cp.setPostText(request.getParameter("elm1"));
            cp.setPostTitle(request.getParameter("title"));

            if (fbPost.updatePost(cp) != null) {
                request.setAttribute("message", "post updated succsesfully!");
                request.setAttribute("returnToURL", "singlepost.do?postId=" + cp.getPostId());
            } else {
                request.setAttribute("message", "updateing the post failed :( please retry");
                request.setAttribute("returnToURL", "editExistingPost.do?postId=" + cp.getPostId());
            }
            request.getRequestDispatcher("message.jsp").forward(request, response);
        } else if (fbs == null) {
            response.sendRedirect("login.jsp");
        } else if (cp == null) {
            request.setAttribute("message", "post that you are trying to edit doesn't exsits(invalid post Id)");
            request.setAttribute("returnToURL", "index.jsp");
            request.getRequestDispatcher("message.jsp").forward(request, response);
        } else if (!cp.getUserID().equals(fbs.getUser())) {
            request.setAttribute("message", "you donn't have permission to edit this post!");
            request.setAttribute("returnToURL", "index.jsp");
            request.getRequestDispatcher("message.jsp").forward(request, response);
        } else {
            request.setAttribute("message", "unexpected error please tell the admins!");
            request.setAttribute("returnToURL", "feedback.jsp");
            request.getRequestDispatcher("message.jsp").forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
